<?php
class upload_imageMod extends commonMod {

	public function index(){
		$data_return_array = array ();
		$files=$_FILES;
		$ban_ext=array('php','asp','asp','html','htm','js','shtml','txt','aspx');
		if(!empty($files)){
			foreach($files as $file) {
				$name=$file['name'];
				$ext=explode('.', $file['name']);
				$ext=end($ext);
				$name=$ext[0];
				if(in_array($ext, $ban_ext)){
					$this->respondError ( 201, '非法文件格式' );
					return;
					$data_return = $this->JSON ( $data_return_array );
					die ( $data_return );
					exit ();
				}
			}
		}else{
			$this->respondError ( 201, '请选择上传文件' );
			return;
			$data_return = $this->JSON ( $data_return_array );
			die ( $data_return );
			exit ();
		}
		$name = hash ( 'sha256', $name);
		$str1 = substr($name,0,1);
		$str2 = substr($name,1,1);
		$str3 = substr($name,2,1);
		//文件路径
		$file_path = __ROOTDIR__ . '/media/images/';
		//文件URL路径
		$file_url = __ROOTDIR__ . '/media/images/';
		//文件目录时间
		//$filetime='';
		$filetime = $str1.'/'.$str2.'/'.$str3;
		//var_dump($filetime);
		//重命名
		function filename(){
			foreach($_FILES as $file) {
				$name=explode('.', $file['name']);
				$ext=end($name);
				$name=$name[0];
			}
			$name = hash ( 'sha256', $name);
			$str1 = substr($name,0,1);
			$str2 = substr($name,1,1);
			$str3 = substr($name,2,1);
			
			$name = substr($name,3);
			if(file_exists(__ROOTDIR__.'/media/images/'.$str1.'/'.$str2.'/'.$str3.'/'.$name.'.'.$ext)){
				$rand='-'.substr(cp_uniqid(),-5);
			}
			return $name.$rand;
		}

		//上传
		$upload = new UploadFile();
		$upload->maxSize = 1024 * 1024 * 2; //大小
		$upload->allowExts = explode(',', $this->config['ACCESSPRY_TYPE']); //格式
		$upload->savePath = $file_path . $filetime . '/'; //保存路径
		$upload->saveRule = 'filename'; //重命名

		if(!$upload->upload())
		{
			$this->respondError ( 201, '上传失败' );
			return;
							
			$data_return = $this->JSON ( $data_return_array );
			die ( $data_return );
			exit ();
		}else{
			$info = $upload->getUploadFileInfo();
			$info = $info[0];
			//返回信息 Array ( [0] => Array ( [name] => 未命名.jpg [type] => image/pjpeg [size] => 53241 [key] => Filedata [extension] => jpg [savepath] => ../../../images/2011-12-17/ [savename] => 1112170727041127335395.jpg ) )
			$ext=$info['extension'];

			 

			
// 			//设置高度和宽度
			
// 			$thumbwidth=$this->config['THUMBNAIL_MAXWIDTH'];
			
// 			$thumbheight=$this->config['THUMBNAIL_MAXHIGHT'];
			
// 			$thumb_cutout=intval(1);
			
// 			//过滤不支持格式进行缩图
// 			if($ext=='jpg'||$ext=='jpeg'||$ext=='png'||$ext=='gif'){
// 				$thumb=Image::thumb($file_path.$filetime.'/'.$info['savename'],$file_path.$filetime.'/thumb_'.$info['savename'],'',$thumbwidth,$thumbheight,'',$thumb_cutout);
// 			}
			

			//根据缩图返回数据
			$file=$str1.$str2.$str3.$info['savename'];
			
			$title=str_replace('.'.$info['extension'],'', $info['name']);
			$json=array('error' => 0, 'url' =>$file, 'original'=>$file_url . $filetime . '/'.$info['savename'], 'file'=>$file,'title'=>$title,'ext'=>$ext,'msg'=>'成功');
			
			
			
			//$data_return_array ['result'] = "1";
			//$data_return_array ['msg'] = "";
			//$url = $this->config['IMG_URL'].'media/images/'.$file;
			//$data_return_array ['data']['url'] =  hash ( 'sha256', $url); // SHA265でハッシュ化
			if($_POST['test']){
				var_dump($url);
			}
			$data_return_array['imageid'] = $file;
			$this->respondSuccess($data_return_array,$_GET['_action']);
			 
		}
	}
	
	public function test(){
		$this->display("upload");
	}
}
?>